package at.sysarch.filter;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.renderable.ParameterBlock;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.beans.PropertyEditor;

import javax.media.jai.InterpolationNearest;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;

import at.sysarch.filter.ROIFilter;

import com.sun.media.jai.widget.DisplayJAI;

public class RectangleEditor extends MouseAdapter implements PropertyEditor {

		private PropertyChangeSupport support;
		private DisplayJAI _display;
		private Rectangle _rectangle;
		private int _x;
		private int _y;
		private int _width;
		private int _height;
		
		float widthRatio;
		float heightRatio;
		
		public RectangleEditor() {
			support = new PropertyChangeSupport(this);
		}

		@Override
		public String getAsText() {
			return "";
		}

		@Override
		public Component getCustomEditor() {

			_display = new DisplayJAI() {

				private static final long serialVersionUID = 1L;

				@Override
				public synchronized void paintComponent(Graphics g) {
					super.paintComponent(g);
					
					if (_width != 0 && _height != 0) {
						g.setColor(Color.RED);
						g.drawRect(_x, _y, _width, _height);
					}
				}
			};
			_display.setSize(300, 300);
			_display.addMouseListener(this);
			_display.addMouseMotionListener(this);
			if (ROIFilter.IMAGE != null) {
				
				int width = ROIFilter.IMAGE.getWidth();
				float ratio = (float) ((float)300 / width);
				
				ParameterBlock pb = new ParameterBlock();
				pb.addSource(ROIFilter.IMAGE); // The source image
				pb.add(ratio); // The xScale
				pb.add(ratio); // The yScale
				pb.add(0.0F); // The x translation
				pb.add(0.0F); // The y translation
				pb.add(new InterpolationNearest()); // The interpolation
				
				// Create the scale operation
				PlanarImage result = JAI.create("scale", pb, null);
				
				widthRatio = ratio;
				heightRatio = ratio;
				_display.set(result);
			}
			return _display;
		}

		@Override
		public String getJavaInitializationString() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public String[] getTags() {
			return null;
		}

		@Override
		public Object getValue() {
			return _rectangle;
		}

		@Override
		public boolean isPaintable() {
			return true;
		}

		@Override
		public void paintValue(Graphics gfx, Rectangle box) {
			gfx.drawString("View image", 0, 22);
		}
		
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
		}

		@Override
		public void setValue(Object value) {

			_rectangle = (Rectangle) value;
		}

		@Override
		public boolean supportsCustomEditor() {

			return true;
		}

		@Override
		public void addPropertyChangeListener(PropertyChangeListener listener) {

			support.addPropertyChangeListener(listener);
		}

		@Override
		public void removePropertyChangeListener(PropertyChangeListener listener) {

			support.removePropertyChangeListener(listener);
		}
		
		@Override
		public void mousePressed(MouseEvent e) {
			_x = e.getX();
			_y = e.getY();
			_display.repaint();
		}

		@Override
		public void mouseReleased(MouseEvent e) {

			_rectangle = new Rectangle(Math.round(_x/widthRatio), Math.round(_y/heightRatio), Math.round(_width/widthRatio), Math.round(_height/heightRatio));
			support.firePropertyChange("", null, null);
			_display.repaint();
		}
		
		@Override
		public void mouseDragged(MouseEvent e) {
			_width = e.getX() - _x;
			_height = e.getY() - _y;
			_display.repaint();
		}
}
